package com.neusoft.resturant.common.utils;

import org.springframework.http.HttpStatus;

import java.util.HashMap;

/**
 * 返回数据
 */
public class R extends HashMap<String, Object> {

    public R() {
        put("code", 200);
        put("msg", "操作成功");
    }

    public static R error() {
        return error(HttpStatus.INTERNAL_SERVER_ERROR.value(), "未知异常，请联系管理员");
    }

    public static R error(String msg) {
        return error(HttpStatus.INTERNAL_SERVER_ERROR.value(), msg);
    }

    public static R error(int code, String msg) {
        R r = new R();
        r.put("code", code);
        r.put("msg", msg);
        return r;
    }

    public static R success(String msg) {
        R r = new R();
        r.put("msg", msg);
        return r;
    }

    public static R success(Object data) {
        R r = new R();
        r.put("data", data);
        return r;
    }

    public static R success() {
        return new R();
    }

    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    public static R toAjax(int rows)
    {
        return rows > 0 ? R.success() : R.error();
    }
}
